// pages/pressure-test-submit/index.js
const { globalData } = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    apply_time: "",
    dateIsError: "",
    contcat_name: "",
    nameIsError: "",
    address: "",
    agentMerchant: {},
    addressIsError: "",
    contcat_phone: "",
    phoneIsError: "",
    merchant_id: "",
    data: "",
    styleSize: { minHeight: 60, maxHeight: 60 },
    fileList: [],
    annex: [],
    memo: "",

  },
  handleNameInput(event) {
    const value = event.detail;

    this.setData({
      nameIsError: value ? "" : "请输入联系人",
    });
  },

  afterRead(event) {
    const image = event.detail.file;

    wx.ajax.uploadFile({
      filePath: image.url,
      callback: (url) => {
        if (url) {
          const { fileList, annex } = this.data;

          fileList.push(url);
          annex.push(image);

          console.log(annex);

          this.setData({
            annex,
            fileList,
          });
        }
      },
    });
  },

  handleDelete(event) {
    const index = event.detail.index;
    const { annex, fileList } = this.data;

    annex.splice(index, 1);
    fileList.splice(index, 1);

    this.setData({
      annex,
      fileList,
    });
  },

  handlePhoneInput(event) {
    const value = event.detail;

    this.setData({
      phoneIsError: value ? "" : "请输入联系电话",
    });
  },

  handleAddressInput(event) {
    const value = event.detail;

    this.setData({
      addressIsError: value ? "" : "请输入地址",
    });
  },

  bindDateChange(event) {
    const { value } = event.detail;

    this.setData({
      apply_time: value,
      dateIsError: value ? "" : "请选择时间",
    });
  },

  handleSubmit() {
    const {
      apply_time,
      contcat_name,
      contcat_phone,
      address,
      memo,
      fileList,
      merchant_id,
      data,
    } = this.data;

    if (!contcat_name) {
      this.setData({
        nameIsError: "请输入联系人",
      });

      return;
    }

    if (!contcat_phone) {
      this.setData({
        phoneIsError: "请输入联系电话",
      });

      return;
    }

    if (!address) {
      this.setData({
        addressIsError: "请输入地址",
      });

      return;
    }

    if (!apply_time) {
      this.setData({
        dateIsError: "请选择时间",
      });

      return;
    }

    const params = {
      id: data.id,
      merchant_id,
      contcat_name,
      contcat_phone,
      address,
      apply_time,
      memo,
      annex: fileList.join(","),
    };

    wx.ajax.post("updatePressureApply.action", params, true).then((res) => {
      if (res.success) {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000,
        });

        const timer = setTimeout(() => {
          wx.navigateBack({
            delat: 1,
          });

          clearTimeout(timer);
        }, 1000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000,
        });
      }
    });
  },

  getData(id) {
    wx.ajax.post("queryPressureById.action", { id }, true).then((res) => {
      // console.log(res);
      if (res.success) {
        const result = res.result;
        if (result.agentMerchant && result.agentMerchant.merchant_name) {
          this.setData({
            agentMerchant: result.agentMerchant,
          });
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.agentid) {
      this.getData(options.agentid);
    }

    if (options.data) {
      const data = JSON.parse(options.data);
      if (data.annex) {
        const arr = data.annex.split(",");

        this.setData({
          fileList: arr,
          annex: arr.map((item) => {
            return {
              url: `${globalData.imageUrl}${item}`,
            };
          }),
        });
      }

      this.setData({
        merchant_id: options.id,
        data: data,
        contcat_name: data.contcat_name,
        contcat_phone: data.contcat_phone,
        address: data.address,
        memo: data.memo,
        apply_time: data.apply_time,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
